package com.jay.android;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class FillinNumberActivity extends Activity implements TextWatcher {

	private EditText numberEditText;
	private EditText identifyEditText;
	private Button nextButton;
	private Button resendButton;
	private String ID = "8666118d7784";
	private String KEY = "274ccccea7bd1428208edf386f41d7b8";
	private int second = 60;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
				WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fillin_number);
		resourceInit();
	}

	@Override
	protected void onDestroy() {
		SMSSDK.unregisterAllEventHandler();
		super.onDestroy();
	}

	void resourceInit() {
		numberEditText = (EditText) findViewById(R.id.number_edit);
		numberEditText.addTextChangedListener(this);
		nextButton = (Button) findViewById(R.id.next_button);
		// nextButton.setEnabled(false);
		nextButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				// Intent intent = new Intent(FillinNumberActivity.this,
				// MessageIdentifyActivity.class);
				// intent.putExtra("number", numberEditText.getEditableText()
				// .toString());
				// startActivity(intent);
				SMSSDK.submitVerificationCode("86", numberEditText.getText()
						.toString(), identifyEditText.getText().toString());
			}
		});

		identifyEditText = (EditText) findViewById(R.id.identify_edit);
		identifyEditText.addTextChangedListener(this);
		resendButton = (Button) findViewById(R.id.resend_btn);
		resendButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				resendButton.setEnabled(false);
				SMSSDK.getVerificationCode("86", numberEditText.getText()
						.toString());
				resendButton.setText("重新发送(" + second + ")");
				new Thread(new Runnable() {
					@Override
					public void run() {
						for (; second > 0; second--) {
							handler.sendEmptyMessage(-9);
							if (second <= 0) {
								break;
							}
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						handler.sendEmptyMessage(-8);
					}
				}).start();
			}
		});
		resendButton.setEnabled(false);

		SMSSDK.initSDK(this, ID, KEY);
		EventHandler eventHandler = new EventHandler() {
			@Override
			public void afterEvent(int event, int result, Object data) {
				// TODO Auto-generated method stub
				Message msg = new Message();
				msg.arg1 = event;
				msg.arg2 = result;
				msg.obj = data;
				handler.sendMessage(msg);
			}
		};
		SMSSDK.registerEventHandler(eventHandler);
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			System.out.println("what = " + msg.what);
			if (msg.what == -9) {
				resendButton.setText("重新发送(" + second + ")");
			} else if (msg.what == -8) {
				resendButton.setText("获取验证码");
				resendButton.setEnabled(true);
				second = 60;
			} else {
				int event = msg.arg1;
				int result = msg.arg2;
				Object data = msg.obj;
				System.out.println("result = " + result);
				if (result == SMSSDK.RESULT_COMPLETE) {
					if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {// 提交验证码成功
						Toast.makeText(getApplicationContext(), "提交验证码成功",
								Toast.LENGTH_SHORT).show();
					} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
						Toast.makeText(getApplicationContext(), "验证码已经发送",
								Toast.LENGTH_SHORT).show();
					} else {
						((Throwable) data).printStackTrace();
						Toast.makeText(getApplicationContext(), "验证失败",
								Toast.LENGTH_SHORT).show();
					}
				}
			}
		};
	};

	public void backOnClick(View view) {
		finish();
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		if (numberEditText.getEditableText().length() == 11) {
			resendButton.setEnabled(true);
		}
	}

}
